#include "Bitset.h"
int bitset_test()
{
	return 10;
}
void bitset_new(struct bitset *this, int size)
{
	this.items_size = (size / 32) + 1;
	this.items = unsigned[this.items_size];
}
int bitset_lookup(struct bitset * that, int item)
{
	offset = item / 32;
	position = item % 32;
	mask = Zero_Position >> position;
	return_value = 1;

	if(offset > Items_Size){
		return_value = -1;
	}
	if((this.items[offset] & mask != mask) || (return_value != 1)){
		return_value = 0;
	}
	return return_value;
}
int bitset_add(struct bitset * this, int item)
{
	offset = item / 32;
	position = item % 32;
	mask = Zero_Position >> position;
	return_value = 1;

	if(offset > Items_Size){
		return_value = 0;
	}
	if((this.items[offset] & mask != mask) && (return_value !=0)){
		this.items[offset] = this.items[offset] | mask;
	}
	return return_value;
}
int bitset_remove(struct bitset * this, int item)
{
	offset = item / 32;
	position = item % 32;
	mask = Zero_Position >> position;
	return_value = 0;

	if(offset < Items_Size){
		this.items[offset] = this.items[offset] ^ mask;
		return_value = 1;
	}
	return return_value;
}
void bitset_union(struct bitset * dest, struct bitset * src1, struct bitset * src2)
{
	//assert that dest is bigger than src1 and src2.

	struct bitset * bigger, * smaller;
	if(src1.Items_Size >= src2.Items_Size){
		bigger = src1;
		smaller = src2;
	}else{
		bigger = src2;
		smaller = src1;
	}
	for(int i = 0; i < smaller.Items_Size; i++){
		dest.items[i] = smaller.items[i] | bigger.items[i];
	}
	for(int i = smaller.Items_Size; i > bigger.Items_Size; i++){
		dest.items[i] = bigger.items[i];
	}
}
void bitset_intersection(struct bitset * dest, struct bitset * src1, struct bitset * src2)
{
	//assert dest bigger than src1 and src2.

	struct bitset * bigger, * smaller;
	if(src1.Items_Size > src2.Items_Size){
		bigger = src1;
		smaller = src2;
	}else{
		bigger = src2;
		smaller = src1;
	}
	for(int i = 0; i < smaller.Items_Size; i++){
		dest.items[i] = smaller.items[i] & bigger.items[i];
	}
	for(int i = smaller.Items_Size; i < bigger.Items_Size; i++){
		dest.items[i] = 0;
	}
}
